package com.rainbowBoot.login.contraller;

import com.rainbowBoot.config.permission.Permission;
import com.rainbowBoot.login.service.LoginService;
import com.rainbowBoot.utils.SendEmailUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/do/login")
public class LoginContraller {

    @Autowired
    private LoginService loginService;

    private Logger logger = LogManager.getLogger(LoginContraller.class);

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
   @PostMapping(value = "loginController")
    public String login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
       logger.info("用户：" + username + "，密码：" + password + "，正在尝试登陆");
       if(loginService.login(username, password)) {
           logger.info("登陆成功！");
           return "true";
       }
       logger.error("登陆失败！账号密码错误");
       return "false";
    }

    /**
     * 页面跳转
     * @return
     */
    @PostMapping(value = "index")
    @Permission(value = "index")
    public void index(HttpServletResponse response) throws Exception {
        SendEmailUtil sendEmailUtil = new SendEmailUtil(SendEmailUtil.STMP,"gshjd@foxmail.com","zgjaepkdwnvibahf");
        sendEmailUtil.setAddresseeEmail("469722471@qq.com");
        sendEmailUtil.sendEmail("只是测试","测试内容",SendEmailUtil.TEXT);
        response.sendRedirect("/html/user/login");
    }

    @PostMapping(value = "login")
    public void login(HttpServletResponse response) throws IOException {
        response.sendRedirect("/html/user/register.html");
    }
}